//
//  FavoriteViewController.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "FavoriteViewController.h"
#import "FavoriteCell.h"

@interface FavoriteViewController ()
@property (strong, nonatomic) UIButton *rightBtn;
@property (strong, nonatomic) IBOutlet UIView *toolbar;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *toolbarBottom;
@property (strong, nonatomic) IBOutlet UIButton *selectAll;
@property (strong, nonatomic) IBOutlet UILabel *totalNum;
@property (strong, nonatomic)  NSArray<Goods *> *array;
@end

@implementation FavoriteViewController

-  (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self initUI];
}
- (void)initUI{
    self.title = LS(@"收藏");
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn.tag = 203;
    self.rightBtn.frame = CGRectMake(0, StuBarHight + 5, 60, 34);
    [self.rightBtn addTarget:self action:@selector(actionForBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightBtn setTitleColor:[UIColor colorHex:@"#1A1A1A"] forState:UIControlStateNormal];
    [self.rightBtn setTitle:LS(@"管理") forState:UIControlStateNormal];
    [self.rightBtn setTitle:LS(@"完成") forState:UIControlStateSelected];
    UIBarButtonItem *item =[[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
    self.navigationItem.rightBarButtonItem = item;
    
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[FavoriteCell nib] forCellReuseIdentifier:[FavoriteCell ID]];
    NSLayoutConstraint *top = [self.tableView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeTop c: 0];
    NSLayoutConstraint *left = [self.tableView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:15];
    NSLayoutConstraint *right = [self.tableView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:-15];
    NSLayoutConstraint *bottom = [self.tableView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.toolbar a2:NSLayoutAttributeTop c:0];
    [self.view addConstraints:@[top,left,right,bottom]];
}
- (void)loadData{
    [GoodsTool find:GoodsOrderStastusFavorite complete:^(NSArray<Goods *> * _Nonnull result) {
        self.array = result;
    }];
}
- (void)setArray:(NSArray<Goods *> *)array{
    _array = array;
    [self updateData:NO index:self.array.count + 1];
}

- (void)updateData:(BOOL)selected index:(NSInteger)index{
    int num = 0;
    for (int i = 0; i<self.array.count; i++) {
        Goods *model = self.array[i];
        if (self.array.count < index) {
            model.select = selected;
        }else{
            if (i == index) { model.select = !model.select;}
        }
        num = model.select ? num+1:num;
    }
    if (self.array.count > index) {
        self.selectAll.selected = self.array.count == num || (num > 0 && self.selectAll.selected);
    }
    self.totalNum.text = [NSString stringWithFormat:LS(@"已选%d件"),num];
    [self.tableView reloadData];
}

- (IBAction)actionForBtn:(UIButton *)sender {
    if (APP.user.account ==  nil || APP.user.account.length <= 0) {
        return [Toast msg:LS(@"您还未登录，请先去登录")];
    }
    switch (sender.tag){
        case 201:///全选
            sender.selected = !sender.selected;
            [self updateData:sender.selected index:self.array.count+1];
            break;
        case 202:///删除
        {
            self.selectAll.selected = NO;
            NSMutableArray *array = [NSMutableArray array];
            for (Goods *m in self.array) {
                if (m.select == NO) { [array addObject:m]; }
                else{
                    [GoodsTool delete:m complete:nil];
                }
            }
            self.array = array;
        }
        case 203:///完成
            self.selectAll.selected = NO;
            sender.selected = !sender.selected;
            self.toolbarBottom.constant = sender.selected ? 0 : -82;
            [self updateData:NO index:self.array.count+1];
            break;
        default:
            break;
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FavoriteCell *cell = [tableView dequeueReusableCellWithIdentifier:[FavoriteCell ID]];
    WS(self)
    cell.selectBtnLeft.constant = self.rightBtn.selected ? 10 : -20;
    cell.model = self.array[indexPath.section];
    cell.block = ^(UIButton * _Nonnull button) {
        if (button.tag == 201) {
            [weakself updateData:NO index:indexPath.section];
        }
    };
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section == self.array.count - 1 ? 20 : 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == self.array.count - 1){
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor clearColor];
        return view;
    }else{
        return nil;
    }
}

@end
